package com.example.springboot.controller;

import com.example.springboot.common.tools.Layui;
import com.example.springboot.common.vo.TreeMenu;
import com.example.springboot.entity.SysRole;
import com.example.springboot.service.RoleMenuService;
import com.example.springboot.service.SysMenuService;
import com.example.springboot.service.SysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysRole)表控制层
 *
 * @author makejava
 * @since 2021-09-06 11:55:40
 */
@RestController
@RequestMapping("URMS/sysRole")
public class SysRoleController {
	/**
	 * 服务对象
	 */
	@Resource
	private SysRoleService sysRoleService;
	@Resource
	private RoleMenuService roleMenuService;

	@Resource
	private SysMenuService sysMenuService;

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("selectOne")
	public SysRole selectOne(Long id) {
		return this.sysRoleService.queryById(id);
	}

	/**
	 * 向数据库查询一页信息
	 *
	 * @return 数据列表
	 */
	@RequestMapping(value = "selectRoleList", method = RequestMethod.GET)
	public Layui selectRoleList(@RequestParam("page") int page, @RequestParam("limit") int limit) {
		List<SysRole> roleList = this.sysRoleService.queryAllByLimit((page - 1) * limit, page * limit);
		return Layui.data(roleList.size(), roleList);
	}

//	/**
//	 * 向数据库查询一页信息
//	 *
//	 * @param pageUtils 分页信息
//	 * @return 数据列表
//	 */
//	@RequestMapping(value = "selectPageList", method = RequestMethod.GET)
//	public Layui selectPageList(@RequestParam PageUtils pageUtils) {
//		List<SysRole> roleList = this.sysRoleService.queryAllByLimit((pageUtils.getPage() - 1) * pageUtils.getLimit(), pageUtils.getPage() * pageUtils.getLimit());
//		int count = this.sysRoleService.queryUserCount();
//		PageUtils pageUtil = new PageUtils(roleList, count, pageUtils.getLimit(), pageUtils.getPage());
//		return Layui.data(pageUtil.getCount(), pageUtil.getList());
//	}

	/**
	 * 向数据库查询一页信息
	 *
	 * @return 数据列表
	 */
	@RequestMapping(value = "selectPermissions", method = RequestMethod.GET)
	public List<TreeMenu> selectPermissions(@RequestParam(value = "id") Long id) {
		return this.sysMenuService.queryTreeRole(id);
	}


	/**
	 * 向数据库添加一条信息
	 *
	 * @param 用户信息
	 * @return 单条数据
	 */
	@RequestMapping(value = "addSubmit", method = RequestMethod.POST)
	public boolean addSubmit(@RequestBody SysRole sysRole, @RequestParam(value = "list") List<Long> list) {
		System.out.println(sysRole.toString());
		System.out.println(list.get(0));

		return roleMenuService.update(sysRole, list);
	}

	/**
	 * 更新数据库一条信息
	 *
	 * @param sysRole 账号信息
	 * @return 数据列表
	 */
	@RequestMapping(value = "editSubmitPermissions", method = RequestMethod.POST)
	public boolean editSubmit(@RequestParam(value = "id") Long id, @RequestParam(value = "list") List<Long> list) {
		System.out.println(list.get(0));
		System.out.println(id);
		SysRole sysRole = new SysRole();
		sysRole.setId(id);
		return roleMenuService.update(sysRole, list);
	}

	/**
	 * 更新数据库一条信息
	 *
	 * @param sysRole 账号信息
	 * @return 数据列表
	 */
	@RequestMapping(value = "editSubmitRole", method = RequestMethod.POST)
	public boolean editSubmit(SysRole sysRole) {
		return sysRoleService.update(sysRole);
	}

	/**
	 * 查询全部数据
	 *
	 * @return 所有菜单数据
	 */
	@RequestMapping(value = "treeMenu", method = RequestMethod.GET)
	public List<TreeMenu> treeMenu() {
		return this.sysRoleService.queryTreeMenu(0L);
	}

	/**
	 * 根据id数据库一条信息
	 *
	 * @param id 主键信息
	 * @return 数据列表
	 */
	@RequestMapping(value = "deleteRole", method = RequestMethod.POST)
	public boolean deleteUser(Long id) {
		return this.sysRoleService.deleteById(id);
	}


}